// pages/store/pay.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        post: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({
            title: '加载中...',
        })
        this.setData(options)
        app.http.get('order/supper-by-member', {
            data: {
                sid: this.data.sid
            }
        }).then(res => {
            this.setData({ supper: res.data })
            wx.hideLoading()
        }).catch(err => {

        })
    },
    onShow() {
        this.update()
    },
    update() {
        const ps = getCurrentPages()
        const parent = ps[ps.length - 2]
        const { cartData, totalNum, totalPrice } = parent.data
        this.setData({
            cartData,
            totalNum,
            totalPrice,
        })
    },
    onInput: function (evt) {
        var key = evt.currentTarget.dataset.key;
        var val = evt.detail.value;
        this.setData({
            [`post.${key}`]: val
        })
    },
    typeChange(e) {
        this.setData({
            select_supper: e.detail.value,
        })
    },
    submit() {
        if (!this.data.supper.length) {
            if (!this.data.post.name) {
                app.wxToast.warn('请输入收件人姓名');
                return;
            }
            if (!this.data.post.phone) {
                app.wxToast.warn('请输入收件人电话');
                return;
            }
            if (!this.data.post.phone.match(/^1[3|4|5|7|8][0-9]\d{4,8}$/)) {
                app.wxToast.warn('请输入正确手机号');
                return;
            }
            if (!this.data.post.address) {
                app.wxToast.warn('请输入收件人地址');
                return;
            }
        }

        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('order/create', {
            data: {
                sid: this.data.sid,
                data: this.data.cartData.map(o => {
                    const idarr = o.id.split("#")
                    return {
                        goods_id: idarr[0],
                        quantity: o.num,
                        sku_id: idarr[1] || 0,
                    }
                }),
                name: !this.data.supper.length ? this.data.post.name : "",
                phone: !this.data.supper.length ? this.data.post.phone : "",
                address: !this.data.supper.length? this.data.post.address : "",
                store_id: this.data.supper.length ? this.data.supper[this.data.select_supper].store_id : 0,
                remark: this.data.post.remark ? this.data.post.remark : "",
                price: this.data.totalPrice,
                sale_member_id: this.data.sell_member_id ? this.data.sell_member_id : ''
            }
        }).then(res => {
            wx.hideLoading()
            // setInterval(() => {
            //     wx.showToast({
            //         title: '提交成功',
            //         icon: 'success',
            //         duration: 2000
            //     })
            // }, 1000)
            wx.navigateTo({
                url: 'order?supplier_id=' + res.data.supplier_id + '&order_id=' + res.data.supplier_order_id
            })

        }).catch(err => {
            app.wxToast.error(err.message)            
        })
    }
})